home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5247 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: 9 Feb 1996 05:49:48 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4fen9s$jjg@news.iag.net>
  8. References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net> <fcusack-0202961621470001@mudskipper.cac.psu.edu> <4f0rjn$sfh@news.iag.net> <823615465snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: pm2-orl23.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <823615465snz@genesis.demon.co.uk>, fred@genesis.demon.co.uk 
  13. says...
  14. >
  15. >In article <4f0rjn$sfh@news.iag.net> jatmon@iag.net "John R Buchan" writes:
  16. >
  17. >>You are correct, of course.  Stupidity on my part. I tend to get careless 
  18. >>about strcmp's reverse logic (ie returns False, when the strings match), 
  19. when
  20. >>I am not paying attention (not an excuse, just an explanation).  I normally 
  21. >>compare the return value to 0, just to keep myself straight.  
  22. >
  23. >
  24. >That's the point though - strcmp() simply doesn't return a boolean value so
  25. >saying 'ie returns False' is just plain wrong and is what gets you into
  26. >trouble. strcmp() is in some sense more like a difference operation. e.g.
  27. >(ignoring overflow) if you take 2 integers a and b and calculate a-b the
  28. >result is >0 if a is larger, ==0 if they are equal and <0 if a is smaller,
  29. >just as strcmp(sa,sb) is defined. IMHO the definition of strcmp is quite
  30. >natural (but maybe the name doesn't quite reflect that definition).
  31.  
  32. <sigh> Yes, I am well aware that strcmp doesn't return a boolean value. When
  33. it is actually used to determine the relative value of strings (ie in a sort
  34. function), it makes perfect sense and its use is quite straightforward.
  35.  
  36. However, when you use it as the condition of an if (or any similar 
  37. conditional) the only thing that matters is whether the return is zero or 
  38. non-zero (False or True).  I guess it's a weakness of mine, but I tend to 
  39. read conditions (or any essentially boolean expressions) in terms of True and
  40. False (an unfortunate side effect of this is that I can make the mistake 
  41. mentioned above).  When the function's name doesn't make its 'boolean' value 
  42. obvious, I find the code much easier to read, if I compare the return to 
  43. some 'obvious' value (eg, 0, NULL, etc).
  44.  
  45. As you indicated the name 'strcmp' doesn't really read well in a boolean. 
  46. Maybe it should changed to something like:
  47.  
  48.  if( theseStringsAreDifferent(str1, str2) ) /* Is this a bit wordy? :-} */
  49.  
  50. ....but that name doesn't make sense in a relative value statement. Hmm..:)
  51.  
  52. -- 
  53. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  54. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  55.  
  56.